﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace WFDAL
{
    public class Systems
    {
        OracleCommand ocm = new OracleCommand();
        NewConverter nc = new NewConverter();
        /// <summary>
        /// 取得系统菜单的实体类集合
        /// </summary>
        /// <returns></returns>
        public string GetUserfolders(string jsonStr)
        {
            Dictionary<string, string> _dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonStr);
            string Sql = "";//ocm.BuildSql(MySqlType.select, "wf_userfolders ", null, _dictionary);
            Sql = string.Format("select   us.* ,(select foldernm From wf_userfolders uus where us.parefolderid=uus.userfoldersid ) parentName From wf_userfolders us where 1=1 ");
            foreach (KeyValuePair<string, string> kp in _dictionary)
            {
                if (kp.Value != "")
                    Sql = Sql + string.Format(" and  {0} in ('{1}')", kp.Key, kp.Value.Replace(",", "','"));
            }
            return NewConverter.DataTableToJson(ocm.Query(Sql));
        }
        public string UpdateUserfolder(string jsonStr)
        {
            string tableName = "wf_userfolders";
            jsonStr = jsonStr.Replace("[", "").Replace("]", "");
            Dictionary<string, string> _dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonStr);

            KeyValuePair<string, string> sqlfld = (KeyValuePair<string, string>)_dictionary.Single(s => s.Key == "USERFOLDERSID");
            Dictionary<string, string> DicWhe = new Dictionary<string, string>();
            DicWhe.Add(sqlfld.Key, sqlfld.Value);

            string Sql = ocm.BuildSql(MySqlType.update, tableName, null, _dictionary, DicWhe);
            if (!ocm.ExecuteSQL(Sql)) return "update  " + "wf_userfolders" + " error ";



            //先删除菜单 群组资料
            deleteRulesysFolders(sqlfld.Value);

            KeyValuePair<string, string> sqlRule = (KeyValuePair<string, string>)_dictionary.Single(s => s.Key == "GROUPID");
            foreach (string item in sqlRule.Value.Split(','))
            {
                //再新增菜单群组资料
                InsertRulesysFolders("{userfoldersid:'" + sqlfld.Value + "'," + "rule_id:'" + item + "'}");
            }

            return "Y";
        }
        public string DeleteUserfolder(string userfoldersid)
        {
            string tableName = "wf_userfolders";

            //先修改wf_userfolders
            string Sql = string.Format("delete {0} where userfoldersid in({1}) ", tableName, userfoldersid);
            if (!ocm.ExecuteSQL(Sql)) return "delete   " + "wf_rulesysfolders" + " error ";
            //再删除wf_rulesysfoders
            return deleteRulesysFolders(userfoldersid);
        }
        //直接删除wf_rulesysfoders
        public string deleteRulesysFolders(string userfoldersid)
        {
            string tableName = "wf_rulesysfolders";
            string Sql = string.Format("delete {0} where userfoldersid in ({1})", tableName, userfoldersid);
            if (!ocm.ExecuteSQL(Sql)) return "delete " + tableName + " error ";
            return "Y";
        }
        //新增wf_rulesysfoders
        public string InsertRulesysFolders(string jsonStr)
        {
            string tableName = "wf_rulesysfolders";
            string Sql = "";
            Dictionary<string, string> _colRule = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonStr);
            Sql = ocm.BuildSql(MySqlType.insert, tableName, null, _colRule, null);
            if (!ocm.ExecuteSQL(Sql)) return "add " + tableName + " error ";
            return "Y";
        }
        public string InsertUserfolder(string jsonStr)
        {
            string tableName = "wf_userfolders";
            jsonStr = jsonStr.Replace("[", "").Replace("]", "");
            //Dictionary<string, string> _dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonStr);
            try
            {
                Dictionary<string, string> _dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonStr);
                string Sql = ocm.BuildSql(MySqlType.insert, tableName, null, _dictionary, null);
                if (!ocm.ExecuteSQL(Sql)) return "add " + tableName + " error ";
                Dictionary<int, string> _col = new Dictionary<int, string>();
                _col.Add(1, "userfoldersid");
                Sql = ocm.BuildSql(MySqlType.max, tableName, _col, null, null);
                string userFoldersid = ocm.QuerySclare(Sql);
                string Groupid = _dictionary["GROUPID"];
                foreach (string s in Groupid.Split(','))
                {
                    InsertRulesysFolders("{RULE_ID:'" + Groupid + "',USERFOLDERSID:'" + userFoldersid + "'}");
                }
                return "Y";
            }
            catch (Exception ex)
            {
                return ex.ToString();
                //throw;
            }

        }
    }
}
